<?php
/**
 * This file serves as a bootstrap so the SWerv application can be tested using PHPUnit.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Framework
 * @subpackage Tests
 */

// make sure a timezone is set
if (function_exists('date_default_timezone_set')) {
	date_default_timezone_set('America/Chicago');
}

// Turn off xdebug if running; we'll display our own error messages, thanks!
if (function_exists('xdebug_disable')) {
	xdebug_disable();
}

if (!defined('TEST_ROOT')) {
	/**
	 * This should be the direct path to the testing directory.  Set automatically -- only needs to be changed if you move things around.
	 */
	define('TEST_ROOT', dirname(dirname(__FILE__)));
}

if (!defined('PATH_BASE')) {
	/**
	 * This should be the direct path to the directory which holds all of the directories of this application, framework, and shared files.
	 * Do not add a slash to the end.  In most cases, this can be auto-detected and should not need to be changed.
	 */
	define('PATH_BASE', dirname(dirname(dirname(dirname(__FILE__)))));
}

if (!defined('PATH_FRAMEWORK')) {
	/**
	 * This should be the direct path to the directory which holds all of the directories of the framework.
	 * Do not add a slash to the end.  In most cases, this can be auto-detected and should not need to be changed.
	 */
	define('PATH_FRAMEWORK', dirname(dirname(dirname(__FILE__))));
}

if (!defined('IS_TEST')) {
	/** 
	 * If set to true, all database connections will use the test database.
	 */
	define('IS_TEST', true);
}

if (!defined('IS_CLI_SCRIPT')) {
	/**
	 * Whether or not this is a script which is being executed from the command line -- in this case, it is.
	 */
	define('IS_CLI_SCRIPT', true);
}

/**
 * Whether or not caching should be used.  This needs to be set to false here in order to test the Cache class.
 */
define('DISABLE_CACHE', false);

/**
 * An array of paths to framework files.
 * @global array $paths
 */
$paths = array(
	'app' => array(
		'protocols'		=>	TEST_ROOT . '/Fixtures',
	),
	'framework' => array(
		'core'			=>	PATH_FRAMEWORK . '/core',
		'exceptions'	=>	PATH_FRAMEWORK . '/exceptions',
		'library'		=>	PATH_FRAMEWORK . '/library',
		'tests'			=>	PATH_FRAMEWORK . '/tests',
	),
	'shared' => array(
		'protocols'		=>	TEST_ROOT . '/Fixtures',
	),
);

/**
 * Include the AutoloadException class because it is used by the autoloader.
 */
require_once($paths['framework']['exceptions'] . '/AutoloadException.php');

/**
 * Include the MemcacheException class because it is used by the Cache class.
 */
require_once($paths['framework']['exceptions'] . '/MemcacheException.php');

/**
 * Include the CacheDisabledException class because it is used by the Cache class.
 */
require_once($paths['framework']['exceptions'] . '/CacheDisabledException.php');

/**
 * Include the StringUtility class as this is pretty much used all over the app and we don't have an autoloader setup yet.
 */
require_once($paths['framework']['library'] . '/StringUtility.php');

/**
 * Include the Cache class as this is pretty much used all over the app and we don't have an autoloader setup yet.
 */
require_once($paths['framework']['library'] . '/Cache.php');

/**
 * Include the various error, exception, & autoloader class.
 */
require_once($paths['framework']['library'] . '/Handler.php');

set_error_handler('Handler::cliErrors');
set_exception_handler('Handler::cliExceptions');

// set the autoloader
if (function_exists('__autoload')) {
	spl_autoload_register('__autoload');
}
spl_autoload_register('Handler::autoloading');

// load all the config files for the framework
$di = new DirectoryIterator(TEST_ROOT . '/Config');
foreach ($di as $configFile) {
	$filename = $configFile->getFilename();
	if (!$configFile->isDot() && substr($filename, strlen($filename) - 4, 4) == '.php') {
		require(TEST_ROOT . '/Config/' . $filename);
	}
}
$di = null;
unset($di);
